package io.github.talelin.latticy.service.oms.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.github.talelin.autoconfigure.exception.ForbiddenException;
import io.github.talelin.latticy.dto.oms.inbound_order.ReceivingSkuDTO;
import io.github.talelin.latticy.model.oms.InboundPackagesItemsDO;
import io.github.talelin.latticy.mapper.oms.InboundPackagesItemsMapper;
import io.github.talelin.latticy.service.oms.InboundPackagesItemsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.latticy.vo.ReceivingRecordVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 入库单子项表 服务实现类
 * </p>
 *
 * @author generator@TaleLin
 * @since 2022-05-18
 */
@Service
public class InboundPackagesItemsServiceImpl extends ServiceImpl<InboundPackagesItemsMapper, InboundPackagesItemsDO> implements InboundPackagesItemsService {

    @Override
    public List<InboundPackagesItemsDO> getPackagesItemsByPackageNoList(List<String> packagesNoList) {
        QueryWrapper<InboundPackagesItemsDO> packagesItemsWrapper = new QueryWrapper<>();
        packagesItemsWrapper.lambda().in(InboundPackagesItemsDO::getPackageNo, packagesNoList);
        return this.baseMapper.selectList(packagesItemsWrapper);
    }

    @Override
    public Map<String, List<InboundPackagesItemsDO>> getPackagesItemsMapByPackageNoList(List<String> packagesNoList) {
        List<InboundPackagesItemsDO> inboundPackagesItemsDOList = this.getPackagesItemsByPackageNoList(packagesNoList);
        Assert.notEmpty(inboundPackagesItemsDOList,"19006");
        return inboundPackagesItemsDOList.parallelStream().collect(Collectors.groupingBy(InboundPackagesItemsDO::getPackageNo));
    }

    @Override
    public boolean batchUpdateReceiveQuantityByIds(List<ReceivingSkuDTO> skus) {
        return this.baseMapper.batchUpdateReceiveQuantityByIds(skus) > 0;
    }

    @Override
    public boolean updateReceiveQuantityByPackageNo(ReceivingRecordVO receivingRecordVO) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("package_no", receivingRecordVO.getPackageNo());
        updateWrapper.setSql("receive_quantity = receive_quantity -" + receivingRecordVO.getReceiveNum());
        updateWrapper.setSql("specimen = specimen -" + receivingRecordVO.getSpecimen());
        updateWrapper.setSql("damaged = damaged -" + receivingRecordVO.getDamaged());
        return this.update(updateWrapper);
    }
}
